package com.yycx.module.bus.provider.handler.haiying.tasktodo.order;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.yycx.bpm.provider.common.handler.ProcessStartHandler;
import com.yycx.bpm.provider.model.ProcessInstanceRequest;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import com.yycx.msg.provider.websocket.WebSocketServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 海鹰启动订单审核流程
 *
 * @author zyf
 */
@Component
@DSTransactional
@Slf4j
public class HyOrderStartProcessHandler implements ProcessStartHandler {

    @Autowired
    private HyOrderService orderService;
    @Resource
    private BusinessUtils businessUtils;

    private Boolean syncTag = true;

    /**
     * 初始化流程参数
     *
     * @param processInstanceRequest
     * @return
     */
    @Override
    public String init(ProcessInstanceRequest processInstanceRequest) {
        processInstanceRequest.setProcessKey("process_order_flp");
        processInstanceRequest.setTenantId("flp");
        return processInstanceRequest.getBusinessKey();
    }

    /**
     * 设置全局流程变量
     *
     * @param processInstanceRequest
     * @param definition
     * @return
     */
    @Override
    public Map<String, Object> initVariables(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition) {
        Map<String, Object> map = new HashMap<>(5);
        String businessKey = processInstanceRequest.getBusinessKey();
        HyOrder order = orderService.getById(businessKey);
        if (!order.getOrderState().equals(0)) {
            ApiAssert.failure("请勿重复提交");
        }
        Integer sendType = order.getSendType();
        ApiAssert.isNotEmpty("发货方式不能为空", sendType);
        map.put("orderNo", order.getOrderNo());
        map.put("companyId", order.getCompanyId());
        map.put("deptId", order.getDeptId());
        return map;
    }

    /**
     * 启动完成业务处理
     *
     * @param processInstanceRequest 请求参数
     * @param definition             流程定义
     * @param processInstance        流程实例
     * @param tasks                  任务列表
     * @param variables              流程变量
     */
    @Override
    @DSTransactional
    public Boolean afterComplete(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition, ProcessInstance processInstance, List<Task> tasks, Map<String, Object> variables) {
        String businessKey = processInstanceRequest.getBusinessKey();
        //List<Long> userIds = roleUserMapper.selectUserIdByRoleCode("ROLE_NQBZ", Long.parseLong(companyId.toString()));
        //socketServerEndpoint.sendMessage(2L, "订单审核", "您有新的订单待审核,单号:" + orderNo, userIds, 2);
        HyOrder order = orderService.getById(businessKey);
        boolean tag=true;
        if (syncTag.equals(false)) {
            //更新在单数
            tag = businessUtils.syncUseNum(order);
        }
        if (tag) {
            //orderService.updateOrderState(Long.parseLong(businessKey), processInstance.getProcessInstanceId(), CommonConstants.ENABLED, CommonConstants.INT_0, 0, DateUtil.date());
            orderService.updateOrderState(Long.parseLong(businessKey), processInstance.getProcessInstanceId(), CommonConstants.ENABLED, CommonConstants.INT_0, 1);
        }
        return tag;

    }
}
